package org.lql.algo.codecrush.hot100.backtrack;

/**
 * @author: liangqinglong
 * @date: 2025-08-30 22:23
 * @description: 79. 单词搜索 <a href="https://leetcode.cn/problems/word-search/description/?envType=study-plan-v2&envId=top-100-liked">...</a>
 **/
public class Exist {

	/**
	 * 给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中，返回 true ；否则，返回 false 。
	 * <p>
	 * 单词必须按照字母顺序，通过相邻的单元格内的字母构成，其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
	 */
	private int m, n;
	private char[][] board;
	private String word;
	private final int[][] directions = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
	private boolean[][] visited;

	public boolean exist(char[][] board, String word) {
		this.m = board.length;
		this.n = board[0].length;
		this.board = board;
		this.word = word;
		this.visited = new boolean[m][n];
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				if (handle(i, j, 0)) {
					return true;
				}
			}
		}
		return false;
	}

	public boolean handle(int sx, int sy, int pos) {
		// 边界检查
		if (sx < 0 || sx >= m || sy < 0 || sy >= n) {
			return false;
		}
		// 检查匹配:已经访问过 ｜｜ 当前字符不匹配
		if (visited[sx][sy] || board[sx][sy] != word.charAt(pos)) {
			return false;
		}
		// 匹配：找到整个单词
		if (pos == word.length() -1) {
			return true;
		}
		visited[sx][sy] = true;
		for (int[] direction : directions) {
			int nx = sx + direction[0];
			int ny = sy + direction[1];
			if (handle(nx, ny, pos + 1)) {
				return true;
			}
		}
		visited[sx][sy] = false;
		return false;
	}



	public static void main(String[] args) {
		char[][] board = new char[][]{
				{'A', 'B', 'C', 'E'},
				{'S', 'F', 'C', 'S'},
				{'A', 'D', 'E', 'E'}
		};
		String word = "ABCCED";
		Exist exist = new Exist();
		System.out.println(exist.exist(board, word));

		System.out.println(exist.exist(new char[][]{{'a'}}, "a"));
		System.out.println(exist.exist(new char[][]{{'a', 'b'}, {'c', 'd'}}, "acdb"));
		System.out.println(exist.exist(new char[][]{{'a', 'b'}, {'c', 'd'}}, "abcd"));
	}
}
